package databasetest;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

public class GuestBookMain extends JFrame {
	//127.0.0.1
	String driver="oracle.jdbc.driver.OracleDriver";
	//	String url="jdbc:oracle:thin:@192.168.10.106:1521:xe";
	String db_user ="hr";
	String db_pwd="hr";

	Connection conn;
	boolean tFlag=true;

	InputG inputG = null;
	SelectG selectG = null;
	UpdateG updateG = null;
	IPaddr ipaddr = null;

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem mntmInput;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GuestBookMain frame = new GuestBookMain();
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public void openinput(){
		if(inputG==null){
			inputG = new InputG(conn,this);
			selectG.setI(inputG);
			inputG.setD(selectG);
			inputG.setBounds(327, 0, 342, 279);
			contentPane.add(inputG);
		}

		inputG.setVisible(true);
		inputG.toFront();
	}
	public void openupdate(){
		if(updateG==null){
			updateG = new UpdateG(conn,this);
			selectG.setD(updateG);
			updateG.setU(selectG);
			updateG.setBounds(0, 280, 448, 428);
			contentPane.add(updateG);
		}

		updateG.setVisible(true);
		updateG.toFront();
	}
	public void openselect(){
		if(selectG==null){
			selectG = new SelectG(conn, this);
			selectG.setBounds(0, 0, 327, 276);
			contentPane.add(selectG);
		}

		selectG.setVisible(true);
		selectG.toFront();
	}
	public void openipaddr(){
		if(ipaddr==null){
			ipaddr = new IPaddr(conn,this);
			ipaddr.setBounds(150, 150, 368, 70);
			contentPane.add(ipaddr);
		}

		ipaddr.setVisible(true);
		ipaddr.toFront();
	}

	public void closeipaddr(){
		if(ipaddr!=null){
			ipaddr.setVisible(false);
		}
	}

	public void ipchange(){
		//		데이터베이스 연결 테스트
		//		1)드라이버로딩
		try{
			InetAddress ia = InetAddress.getLocalHost();
			String a = JOptionPane.showInputDialog(null, "아이피주소를 입력해주세요",ia.getHostAddress());
			String url="jdbc:oracle:thin:@"+a+":1521:xe";
			if(a!=null){
				if(tFlag){
					Class.forName(driver).newInstance();
					System.out.println("드라이버가 로딩되었습니다.");
					tFlag=false;
				}


				//2)Connection 객체 생성
				conn=DriverManager.getConnection(url, db_user,db_pwd);
				System.out.println("데이터베이스가 정상적으로 연결되었습니다.");	

				openselect();

				if(selectG != null){
					selectG.columnadd();
				}


			}
			a=null;

		}catch(Exception ex){
			ex.printStackTrace();
		}
	}


	public GuestBookMain() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 675, 540);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setForeground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		ipchange();
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMenu());
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMenu() {
		if (menu == null) {
			menu = new JMenu("방명록");
			menu.add(getMntmInput());
			menu.add(getMntmNewMenuItem());
			menu.add(getMntmNewMenuItem_1());
		}
		return menu;
	}
	private JMenuItem getMntmInput() {
		if (mntmInput == null) {
			mntmInput = new JMenuItem("InputG");
			mntmInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openinput();
				}
			});
		}
		return mntmInput;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("UpdateG");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openupdate();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("SelectG");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openselect();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("접속");
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("서버연결");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					ipchange();

				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("연결해제");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					try{
						if(conn !=null){
							if(!conn.isClosed()){
								conn.close();
							}
						}

						JOptionPane.showMessageDialog(null, "연결이 해지되었습니다.");
						selectG.removeall();
					}catch(SQLException e){
						e.printStackTrace();
					}

				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
